package com.springSecurity.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@SpringBootApplication
@RestController
@EnableAutoConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启全局权限
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

    /**
     * 项目入口
     * @return
     */
	@GetMapping("/")
	public String home(){
		return "SpringBoot&&SpringSecurity!!";
	}

    /**
     * 测试拦截器 需登录
     * @return
     */
	@GetMapping("/hello")
    public String hello(){
	    return "hello,SpringSecurity!!";
    }

    /**
     * 测试角色访问权限
	 * @PreAuthorize("hasRole('ROLE_ADMIN')")表示只有用户权限为“ADMIN”权限才能访问，PreAuthorize是在方法运行前对方法参数进行校验
	 * @PostAuthorize("returnObject%2==0")表示只有返回参数为偶数时才返回，否则进行过滤，PostAuthorize是对返回值进行校验
     * @return 字符串
     */
    @PreAuthorize("hasRole('ROLE_ADMIN')")
	@PostAuthorize("returnObject.equals('roleAuth')")
    @GetMapping("/role")
    public String role(){
	    return "roleAuth";
    }


	/**
	 *@PreFilter()与@PostFilter()是对集合参数的校验
	 * @PreFilter("filterObject%2==0")表示传进来的参数集合中元素必须能被2整除
	 * @PostFilter("filterObject%4==0")表示返回的参数集合中元素必须能被4整除
	 * @return
	 */
	@PreFilter("filterObject%2==0")
	@PostFilter("filterObject%4==0")
    @GetMapping("/test")
	public List<Integer> test(List<Integer> idList){
    	return idList;
	}

}
